﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ReportForm.BLL
{
    class tReportBuy
    {
        UTLDB.UTLDB connect = new UTLDB.UTLDB();
        DataTable data = new DataTable();
        DataTable dataSQL = new DataTable();

        public tReportBuy()
        { 
            DataColumn[] dc = new DataColumn[]{ 
                                new DataColumn("No", Type.GetType("System.Int32")),
                                new DataColumn("NamePr", Type.GetType("System.String")),
                                new DataColumn("Barcode", Type.GetType("System.String")),
                                new DataColumn("Wgam", Type.GetType("System.Double")),
                                new DataColumn("Wchi", Type.GetType("System.String")),
                                new DataColumn("Price", Type.GetType("System.Double")),

                                new DataColumn("CreateDate", Type.GetType("System.String")),
                                new DataColumn("Status", Type.GetType("System.String")),
                                new DataColumn("begin", Type.GetType("System.String")),
                                new DataColumn("TotalChi", Type.GetType("System.String")),
                                new DataColumn("end", Type.GetType("System.String"))
            };
            
            

            data.Columns.AddRange(dc);
        }

        public DataTable GetBuy(int idCat,string from, string to)
        {
            DataTable data = connect.GetDataByStoredProcedure("sp_SelecttblProductByDateNwew",
               new string[] {"@IdCat", "@DateFrom", "@DateTo" },
               new object[] { idCat,to, from }
                );
            return data;
        }
        public DataTable FormatData(DataTable data)
    {
       return data;
    }
        public string GetTotalChi( DataTable data){
            double temp = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                temp += double.Parse(data.Rows[i]["WChi"].ToString());
            }
            return FormatWeight(temp.ToString());
        }
        public DataTable SetData(int idCat,string from, string to)
        {
            string TotalChi = "";
            int i = 0;
            dataSQL = GetBuy(idCat,from, to);
            TotalChi = GetTotalChi(dataSQL);
            for (i = 0; i < dataSQL.Rows.Count; i++)
            {
                DataRow r = data.NewRow();
                               r["No"]=i+1;
                               r["NamePr"] = dataSQL.Rows[i]["NamePr"];
                               r["Barcode"]= dataSQL.Rows[i]["Barcode"];
                               r["Wgam"]= dataSQL.Rows[i]["WGam"];
                               r["Wchi"]= FormatWeight(dataSQL.Rows[i]["WChi"].ToString());
                               r["Price"]= dataSQL.Rows[i]["PriceShell"];
                               r["CreateDate"]= dataSQL.Rows[i]["CreateDate"];
                               r["Status"] = dataSQL.Rows[i]["Status"];
                               r["begin"] = to;
                               r["TotalChi"] = TotalChi;
                               r["end"] = from;

                data.Rows.Add(r);
            }
            return data;
        }
        public string FormatWeight(string str)
        {
            string size = "";
            string[] tam = str.Trim().Split('.');
            string phanle;
            if (tam.Length > 1) phanle = tam[1];
            else phanle = "";

            switch (tam[0].Length)
            {
                case 1:
                    if (tam[0] == "0") size = 0 + phanle;
                    else size = tam[0] + "L" + phanle;
                    break;
                case 2:
                    size = tam[0].Substring(0, 1) + "P"
                        + tam[0].Substring(1, 1) + "L" + phanle;
                    break;
                case 3:
                    size = tam[0].Substring(0, 1) + "C"
                        + tam[0].Substring(1, 1) + "P" +
                        tam[0].Substring(2, 1) + "L" + phanle;
                    break;
                case 4:
                    size = tam[0].Substring(0, 1) + "L" +
                        tam[0].Substring(1, 1) + "C"
                        + tam[0].Substring(2, 1) + "P" +
                        tam[0].Substring(3, 1) + "L" + phanle;
                    break;

            }
            return size;
        } 

    }
}
